package com.hisoft.processor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

import com.hisoft.constant.XMLTestCaseTag;
import com.hisoft.entity.ABaseTag;
import com.hisoft.entity.Assert;
import com.hisoft.entity.Choose;
import com.hisoft.entity.Environment;
import com.hisoft.entity.Function;
import com.hisoft.entity.Html;
import com.hisoft.entity.Import;
import com.hisoft.entity.Loop;
import com.hisoft.entity.TestCase;
import com.hisoft.entity.Verify;
import com.hisoft.type.ResultEnum;
import com.hisoft.utility.DataProviderTool;
import com.hisoft.xml.SchemaChecker;

/**
 * 生成报告
 * 
 * @author COOLOO
 * 
 */
public class ReportProcessor {
	Logger logger = Logger.getLogger(ReportProcessor.class);
	//html开头
	private static String htmlHead = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";

	/**
	 * 根据已经执行过的testcase对象
	 * <b>生成XML报告和html报告 </b>
	 * 
	 * @throws XMLStreamException
	 * @throws FileNotFoundException
	 * @throws UnsupportedEncodingException
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-8
	 */
	public void XMLAndHTMLReport(List<TestCase> testcaseList) {
		this.writeHtmlFile(this.XMLReport(testcaseList), "schema/Xml2Html.xsl");
	}
	
	/**
	 * 生成XML报告
	 * @param testcaseList
	 * @return
	 */
	private String XMLReport(List<TestCase> testcaseList) {
		int pass = 0;
		int fail = 0;
		int skip = 0;
		long time = 0L;
		long totalTime = 0L;
		int totalPass = 0;
		int totalFail = 0;
		int totalSkip = 0;
		int casePassed = 0;
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
		String pathXml = "output/" + df.format(new Date()) + ".xml";
		logger.info("create XML report('" + pathXml + "').");
		Environment environment = Environment.getInstance();
		try {
			if (!(new File("~/output").isDirectory())) {
				new File("output").mkdir();
			}
		} catch (SecurityException e) {
			logger.error(e.getMessage(), e);
		}
		try {
			PrintWriter writerXml = new PrintWriter(new OutputStreamWriter(new FileOutputStream(pathXml), "utf-8"));
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
			XMLStreamWriter xmlsw = xof.createXMLStreamWriter(writerXml);
			xmlsw.writeStartDocument("utf-8", "1.0");// write the statement of
														// xml
			xmlsw.writeStartElement("Report");
			this.Environment2XML(xmlsw, environment);
			xmlsw.writeStartElement("TestCases");
			for (TestCase tc : testcaseList) {
				ResultEnum re = null;
				xmlsw.writeStartElement("TestCase");
				xmlsw.writeAttribute(XMLTestCaseTag.FILE_NAME, tc.getFileName());
				xmlsw.writeAttribute(XMLTestCaseTag.TEST_CASE_NAME, tc.getTestCaseName());
				xmlsw.writeAttribute(XMLTestCaseTag.TEST_CASE_VERSION, tc.getTestCaseVersion());
				xmlsw.writeAttribute(XMLTestCaseTag.AUTHOR, tc.getLastUpdatedAuthor());
				xmlsw.writeAttribute(XMLTestCaseTag.DATE, tc.getLastUpdatedDate());
				String index = tc.getRecordInfoIndex();
				String account = tc.getRecordInfoAccount();
				if (account == null || account == "") {
				} else {
					if (index == null || account == "") {
					} else {
						xmlsw.writeAttribute("deviceModelName", DataProviderTool.getData("account(" + account + ").device." + index + ".modelName"));
						xmlsw.writeAttribute("deviceSN", DataProviderTool.getData("account(" + account + ").device." + index + ".sn"));
						xmlsw.writeAttribute("deviceConnectionType", DataProviderTool.getData("account(" + account + ").device." + index + ".connectionType"));
						xmlsw.writeAttribute("deviceMac", DataProviderTool.getData("account(" + account + ").device." + index + ".mac"));
						xmlsw.writeAttribute("deviceFW", DataProviderTool.getData("account(" + account + ").device." + index + ".fw"));
					}
					xmlsw.writeAttribute("email", DataProviderTool.getData("account(" + account + ").email"));
					xmlsw.writeAttribute("password", DataProviderTool.getData("account(" + account + ").pwd"));
				}
				for (Object o : tc.getOperateList()) {
					if (o instanceof Html) {
						this.Html2XML(xmlsw, (Html) o);
						re = ((Html) o).getResult();
					} else if (o instanceof Function) {
						this.Function2XML(xmlsw, (Function) o);
						re = ((Function) o).getResult();
					} else if (o instanceof Verify) {
						this.Verify2XML(xmlsw, (Verify) o);
						re = ((Verify) o).getResult();
					} else if (o instanceof Assert) {
						this.Assert2XML(xmlsw, (Assert) o);
						re = ((Assert) o).getResult();
					} else if (o instanceof Choose) {
						this.Choose2XML(xmlsw, (Choose) o);
						re = ((Choose) o).getResult();
					} else if (o instanceof Import) {
						this.Import2XML(xmlsw, (Import) o);
						re = ((Import) o).getResult();
					} else if (o instanceof Loop) {
						this.Loop2XML(xmlsw, (Loop) o);
						re = ((Loop) o).getResult();
					}
					if (re.equals(ResultEnum.PASS)) {
						pass++;
					} else if (re.equals(ResultEnum.FAIL)) {
						fail++;
					} else {
						skip++;
					}
				}
				time = tc.getTime() / 1000;
				totalPass += pass;
				totalFail += fail;
				totalSkip += skip;
				totalTime += time;
				// <TestcaseResult>
				// <name>demo.xml</name>
				// <passed>15</passed>
				// <failed>1</failed>
				// <skipped>0</skipped>
				// <totaltime>42895</totaltime>
				// <result>fail</result>
				// </TestcaseResult>
				xmlsw.writeStartElement("TestcaseResult");
				xmlsw.writeAttribute(XMLTestCaseTag.FILE_NAME, tc.getFileName());
				xmlsw.writeAttribute(XMLTestCaseTag.PASSED, "" + pass);
				xmlsw.writeAttribute(XMLTestCaseTag.FAILED, "" + fail);
				xmlsw.writeAttribute(XMLTestCaseTag.SKIPED, "" + skip);
				xmlsw.writeAttribute(XMLTestCaseTag.TOTAL_TIME, "" + time);
				xmlsw.writeEndElement(); // TestcaseResult
				casePassed += pass / tc.getOperateList().size();
				pass = 0;
				fail = 0;
				skip = 0;
				time = 0L;
				xmlsw.writeEndElement(); // TestCase
			}
			xmlsw.writeEndElement(); // TestCases
			xmlsw.writeStartElement("TestcaseResult");
			xmlsw.writeAttribute(XMLTestCaseTag.PASSED, "" + totalPass);
			xmlsw.writeAttribute(XMLTestCaseTag.FAILED, "" + totalFail);
			xmlsw.writeAttribute(XMLTestCaseTag.SKIPED, "" + totalSkip);
			xmlsw.writeAttribute(XMLTestCaseTag.TOTAL_TIME, "" + totalTime);
			xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + casePassed + " / " + testcaseList.size());
			xmlsw.writeEndElement(); // TestcaseResult
			xmlsw.writeEndElement(); // Report
			xmlsw.flush();
			xmlsw.close();
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage(), e);
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage(), e);
		} catch (FactoryConfigurationError e) {
			logger.error(e.getMessage(), e);
		} catch (XMLStreamException e) {
			logger.error(e.getMessage(), e);
		}
		formatXml(pathXml);
		return pathXml;
	}
	/**
	 * loop对象装化为XML
	 * @param xmlsw
	 * @param o
	 * @throws XMLStreamException
	 */
	private void Loop2XML(XMLStreamWriter xmlsw, Loop o) throws XMLStreamException {
		xmlsw.writeStartElement(XMLTestCaseTag.TAG);
		xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "" + (o.getLoop().size()));
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.LOOP);
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + o.getLineNumb());
		for (ABaseTag abt : o.getLoop()) {
			xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
			if (abt instanceof Html) {
				xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.HTML);
			} else if (abt instanceof Verify) {
				xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.VERIFY);
			} else if (abt instanceof Assert) {
				xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.ASSERT);
			} else if (abt instanceof Function) {
				xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.FUNCTION);
			}
			xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, abt.toString());
			xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + abt.getResult());
			xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + abt.getLineNumb());
			xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, abt.getReportInfo());
			xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, abt.getScreenshot());
			xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "" + (o.getLoop().size()));
			xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (abt.getInfo()));
			xmlsw.writeEndElement();// OPERATEINFO
		}
		xmlsw.writeEndElement();// TAG
	}
/**
 * 格式化xml
 *
 * @param pathXml
 */
	public void formatXml(String pathXml) {
		logger.info("format XML report('" + pathXml + "').");
		try {
			SAXReader saxReader = new SAXReader();
			// 注释：先读入一个文件流
			Document doc = saxReader.read(new FileInputStream(new File(pathXml)));
			// 注释：创建输出格式
			OutputFormat format = OutputFormat.createPrettyPrint();
			// 注释：制定输出xml的编码类型
			format.setEncoding("UTF-8");
			// 注释：创建一个文件输出流
			XMLWriter writer = new XMLWriter(new FileWriter(new File(pathXml)), format);
			// 注释：将格式化后的xml串写入到文件
			writer.write(doc);
			writer.close();
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}
	}

	/**
	 * 
	 * <b>转化xml到html</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-8
	 */
	public void writeHtmlFile(String xmlpath, String sxlpath) {
		File html = new File(xmlpath.replace(".xml", ".html"));
		logger.info("create HTML report('" + html.getPath() + "')");
		File xmlFile = new File(xmlpath);
		File sxlFile = new File(sxlpath);
		PrintWriter pw = null;
		StringWriter sw = null;
		if (SchemaChecker.fileExists(xmlFile) && SchemaChecker.fileExists(sxlFile) && SchemaChecker.validateOnlyXml(xmlFile)) {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			// 根据xml文件，取得相应的名字
			try {
				DocumentBuilder db = dbf.newDocumentBuilder();
				org.w3c.dom.Document doc = db.parse(xmlpath);
				DOMSource source = new DOMSource(doc);
				sw = new StringWriter();
				Result resulted = new StreamResult(sw);
				// StreamResult result = new StreamResult(html);
				StreamSource ss = new StreamSource(sxlpath);
				TransformerFactory tff = TransformerFactory.newInstance();
				Transformer tf = tff.newTransformer(ss);
				tf.setOutputProperty("encoding", "utf-8");// 设置编码格式为"UTF-8"
				// 转化
				tf.transform(source, resulted);
				// 生成html文件
				pw = new PrintWriter(html);
				// 替换"&gt;"为 ">".
				pw.write(htmlHead + sw.toString().replaceAll("&gt;", ">").replace("&lt;", "<").replace("&amp;", "&"));
			} catch (ParserConfigurationException e) {
				logger.error(e.getMessage(), e);
			} catch (SAXException e) {
				logger.error(e.getMessage(), e);
			} catch (IOException e) {
				logger.error(e.getMessage(), e);
			} catch (TransformerConfigurationException e) {
				logger.error(e.getMessage(), e);
			} catch (TransformerException e) {
				logger.error(e.getMessage(), e);
			} finally {
				try {
					if (!(pw == null)) {
						pw.close();
					}
					if (!(sw == null)) {
						sw.close();
					}
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
	}
	/**
	 * 把environment转化为xml
	 *
	 * @param xmlsw
	 * @param environment
	 * @throws XMLStreamException
	 */
	private void Environment2XML(XMLStreamWriter xmlsw, Environment environment) throws XMLStreamException {

		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy/MM/dd a", Locale.ENGLISH);
		String datetime = tempDate.format(new java.util.Date());
		xmlsw.writeStartElement("Configurations");
		xmlsw.writeStartElement("Time");
		xmlsw.writeCharacters("" + "(GMT+8) " + datetime);
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("OS");
		xmlsw.writeCharacters("" + environment.getOs());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("Host");
		xmlsw.writeCharacters("" + environment.getHost());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("Port");
		xmlsw.writeCharacters("" + environment.getPort());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("BaseUrl");
		xmlsw.writeCharacters("" + environment.getBaseUrl());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("Browser");
		xmlsw.writeCharacters("" + environment.getBrowser());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("Language");
		xmlsw.writeCharacters("" + environment.getLanguage());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("Timeout");
		xmlsw.writeCharacters("" + environment.getTimeout());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("RetryTimes");
		xmlsw.writeCharacters("" + environment.getRetryTimes());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("PageDataPath");
		xmlsw.writeCharacters("" + environment.getPageDataPath());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("TestCasePath");
		xmlsw.writeCharacters("" + environment.getTestCasePath());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("LANSpeed");
		xmlsw.writeCharacters("" + environment.getLanSpeed());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("WANSpeed");
		xmlsw.writeCharacters("" + environment.getWanSpeed());
		xmlsw.writeEndElement();
		xmlsw.writeStartElement("SiteSpeed");
		xmlsw.writeCharacters("" + environment.getUrlSpeed());
		xmlsw.writeEndElement();
		xmlsw.writeEndElement();// end the configurations
	}
	/**
	 * html对象转化为xml
	 * @param xmlsw
	 * @param html
	 * @throws XMLStreamException
	 */
	private void Html2XML(XMLStreamWriter xmlsw, Html html) throws XMLStreamException {
		xmlsw.writeStartElement(XMLTestCaseTag.TAG);
		xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "1");
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.HTML);
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + html.getLineNumb());
		xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.HTML);
		xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, html.toString());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + html.getResult());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, html.getReportInfo());
		xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, html.getScreenshot());
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + html.getLineNumb());
		xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (html.getInfo()));
		xmlsw.writeEndElement();// OPERATEINFO
		xmlsw.writeEndElement();// TAG
	}
/**
 * function对象装化为xml
 * @param xmlsw
 * @param function
 * @throws XMLStreamException
 */
	private void Function2XML(XMLStreamWriter xmlsw, Function function) throws XMLStreamException {
		xmlsw.writeStartElement(XMLTestCaseTag.TAG);
		xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "1");
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.FUNCTION);
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + function.getLineNumb());
		xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.FUNCTION);
		xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, function.toString());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + function.getResult());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, function.getReportInfo());
		xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, function.getScreenshot());
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + function.getLineNumb());
		xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (function.getInfo()));
		xmlsw.writeEndElement();// OPERATEINFO
		xmlsw.writeEndElement();// TAG
	}
	/**
	 * verify对象转化为xml
	 * @param xmlsw
	 * @param verify
	 * @throws XMLStreamException
	 */
	private void Verify2XML(XMLStreamWriter xmlsw, Verify verify) throws XMLStreamException {
		xmlsw.writeStartElement(XMLTestCaseTag.TAG);
		xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "1");
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.VERIFY);
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + verify.getLineNumb());
		xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.VERIFY);
		xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, verify.toString());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + verify.getResult());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, verify.getReportInfo());
		xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, verify.getScreenshot());
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + verify.getLineNumb());
		xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (verify.getInfo()));
		xmlsw.writeEndElement();// OPERATEINFO
		xmlsw.writeEndElement();// TAG
	}
/**
 * assert对象转化为xml
 * 
 * @param xmlsw
 * @param as
 * @throws XMLStreamException
 */
	private void Assert2XML(XMLStreamWriter xmlsw, Assert as) throws XMLStreamException {
		xmlsw.writeStartElement(XMLTestCaseTag.TAG);
		xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "1");
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.ASSERT);
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + as.getLineNumb());
		xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.ASSERT);
		xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, as.toString());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + as.getResult());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, as.getReportInfo());
		xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, as.getScreenshot());
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + as.getLineNumb());
		xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (as.getInfo()));
		
		xmlsw.writeEndElement();// OPERATEINFO
		xmlsw.writeEndElement();// TAG
	}
/**
 * do对象转化为xml
 * @param xmlsw
 * @param choose
 * @throws XMLStreamException
 */
	private void Do2XML(XMLStreamWriter xmlsw, Choose choose) throws XMLStreamException {
		List<ABaseTag> list = choose.getDo();
		for (ABaseTag abt : list) {
			xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
			xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.DO);
			xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, abt.toString());
			xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + abt.getResult());
			xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + abt.getLineNumb());
			xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, abt.getReportInfo());
			xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, abt.getScreenshot());
			xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "" + (1 + choose.getDo().size() + choose.getOtherwise().size()));
			xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (abt.getInfo()));
			xmlsw.writeEndElement();// OPERATEINFO
		}
	}
/**
 * otherwise转化为xml对象
 * @param xmlsw
 * @param choose
 * @throws XMLStreamException
 */
	private void Otherwise2XML(XMLStreamWriter xmlsw, Choose choose) throws XMLStreamException {
		List<ABaseTag> list = choose.getOtherwise();
		for (ABaseTag abt : list) {
			xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
			xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.OTHERWISE);
			xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, abt.toString());
			xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + abt.getResult());
			xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + abt.getLineNumb());
			xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, abt.getReportInfo());
			xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, abt.getScreenshot());
			xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "" + (1 + choose.getDo().size() + choose.getOtherwise().size()));
			xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (abt.getInfo()));
			xmlsw.writeEndElement();// OPERATEINFO
		}
	}
/**
 * when对象转化为xml
 * 
 * @param xmlsw
 * @param choose
 * @throws XMLStreamException
 */
	private void When2XML(XMLStreamWriter xmlsw, Choose choose) throws XMLStreamException {
		ABaseTag abt = choose.getWhen();
		xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.WHEN);
		xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, abt.toString());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + abt.getResult());
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + abt.getLineNumb());
		xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, abt.getReportInfo());
		xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, abt.getScreenshot());
		xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "" + (1 + choose.getDo().size() + choose.getOtherwise().size()));
		xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (abt.getInfo()));
		xmlsw.writeEndElement();// OPERATEINFO
	}
/**
 * choose对象转化为xml
 * @param xmlsw
 * @param choose
 * @throws XMLStreamException
 */
	private void Choose2XML(XMLStreamWriter xmlsw, Choose choose) throws XMLStreamException {
		xmlsw.writeStartElement(XMLTestCaseTag.TAG);
		xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "" + (1 + choose.getDo().size() + choose.getOtherwise().size()));
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.CHOOSE);
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + choose.getLineNumb());
		this.When2XML(xmlsw, choose);
		this.Do2XML(xmlsw, choose);
		this.Otherwise2XML(xmlsw, choose);
		xmlsw.writeEndElement();// TAG
	}
/**
 * import转化为xml
 * @param xmlsw
 * @param o
 * @throws XMLStreamException
 */
	private void Import2XML(XMLStreamWriter xmlsw, Import o) throws XMLStreamException {
		xmlsw.writeStartElement(XMLTestCaseTag.TAG);
		xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "" + (o.getImportXML().size()));
		xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.IMPORTXML);
		xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + o.getLineNumb());
		for (ABaseTag abt : o.getImportXML()) {
			xmlsw.writeStartElement(XMLTestCaseTag.OPERATEINFO);
			if (abt instanceof Html) {
				xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.HTML);
			} else if (abt instanceof Verify) {
				xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.VERIFY);
			} else if (abt instanceof Assert) {
				xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.ASSERT);
			} else if (abt instanceof Function) {
				xmlsw.writeAttribute(XMLTestCaseTag.NAME, XMLTestCaseTag.FUNCTION);
			}
			xmlsw.writeAttribute(XMLTestCaseTag.OPERATE, abt.toString());
			xmlsw.writeAttribute(XMLTestCaseTag.RESULT, "" + abt.getResult());
			xmlsw.writeAttribute(XMLTestCaseTag.LINENUMBER, "" + abt.getLineNumb());
			xmlsw.writeAttribute(XMLTestCaseTag.RESULTINFO, abt.getReportInfo());
			xmlsw.writeAttribute(XMLTestCaseTag.SCREENSHOT, abt.getScreenshot());
			xmlsw.writeAttribute(XMLTestCaseTag.COUNT, "" + (o.getImportXML().size()));
			xmlsw.writeAttribute(XMLTestCaseTag.INFO, "" + (o.getInfo()));
			xmlsw.writeEndElement();// OPERATEINFO
		}
		xmlsw.writeEndElement();// TAG
	}
}
